home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 4.0 KB | 171 lines |
- package symantec.itools.multimedia;
-
-
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.MalformedURLException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Vector;
- import sun.audio.AudioStream;
- import sun.audio.AudioDataStream;
- import sun.audio.AudioPlayer;
- import sun.audio.AudioData;
-
-
- /**
- * SoundPlayer component.
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
-
- public class SoundPlayer
- {
- private Vector clips;
- private boolean sync = true;
- private SoundViewerThread spt = null;
- private int repeatCt = 1;
-
- public static final int INFINITE = -1;
-
- public SoundPlayer() {
- clips = new Vector();
- }
-
- public void setSyncMode(boolean f) {
- sync = f;
- if (spt != null)
- spt.doSync(f);
- }
-
- public boolean getSyncMode() {
- return sync;
- }
-
- public void setRepeat(int c) {
- repeatCt = c;
- }
-
- public int getRepeat() {
- return repeatCt;
- }
-
- public void addURL(URL u) {
- InputStream in = null;
- AudioData data;
- try {
- try {
- URLConnection uc = u.openConnection();
- uc.setAllowUserInteraction(true);
- in = uc.getInputStream();
- AudioStream as = new AudioStream(in);
- clips.addElement(new SoundViewerItem(as.getData(),
- (int)(((double)as.getLength() / 7168.0) * 1000.0)));
- } finally {
- if (in != null)
- in.close();
- }
- } catch (IOException e) {
- }
- }
-
- public void addStringURL(String url) {
- try {
- addURL(new URL(url));
- } catch (MalformedURLException e) {
- }
- }
-
- public void setURLList(URL[] list) {
- clips.removeAllElements();
- for (int i = 0; i < list.length; ++i)
- addURL(list[i]);
- }
-
- public URL[] getURLList() {
- int len = clips.size();
- URL[] ret = new URL[len];
- for (int i = 0; i < len; ++i)
- ret[i] = (URL)clips.elementAt(i);
- return ret;
- }
-
- public void play() {
- spt = new SoundViewerThread(clips, sync, repeatCt);
- spt.start();
- }
-
- public void stop() {
- if (spt != null)
- spt.doStop();
- }
-
- public void stop(int delay) {
- try {
- Thread.sleep(delay);
- } catch (InterruptedException ie) {
- }
- stop();
- }
- }
-
- class SoundViewerThread extends Thread
- {
- private Vector clips;
- private boolean doEnd = false;
- private AudioDataStream curStream = null;
- private int repeatCt = 0;
- private boolean sync = true;
-
- SoundViewerThread(Vector clips, boolean sync, int rct) {
- this.clips = clips;
- this.sync = sync;
- repeatCt = rct;
- }
-
- public void run() {
- while (repeatCt == -1 || repeatCt-- > 0) {
- int nClips = clips.size();
- for (int index = 0; index < nClips && !doEnd; ++index) {
- SoundViewerItem spi = (SoundViewerItem)clips.elementAt(index);
- curStream = new AudioDataStream(spi.data);
- AudioPlayer.player.start(curStream);
- if (sync) {
- try {
- Thread.sleep(spi.delay);
- } catch (InterruptedException ie) {
- }
- }
- }
- curStream = null;
- }
- doEnd = false;
- }
-
- void doSync(boolean f) {
- sync = f;
- }
-
- void doStop() {
- if (curStream != null)
- AudioPlayer.player.stop(curStream);
- doEnd = true;
- }
- }
-
-
- class SoundViewerItem
- {
- AudioData data;
- int delay;
-
- SoundViewerItem(AudioData data, int delay)
- {
- this.data = data;
- this.delay = delay;
- }
- }
-
-